package com.easy.dn.ui.usercenter.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.easy.common.base.BaseActivity;
import com.easy.common.baserx.RxBus;
import com.easy.common.baserx.RxSubscriber;
import com.easy.common.commonwidget.DnToolbar;
import com.easy.dn.api.Api;
import com.easy.dn.api.ApiService;
import com.easy.dn.app.AppConstant;
import com.easy.dn.app.GlobalApplication;
import com.easy.dn.bean.InfoRefreshBean;
import com.easy.dn.bean.UserDataBean;
import com.easy.dn.utils.MapHelper;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.Map;

import butterknife.Bind;
import dn.easy.com.easyto.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static dn.easy.com.easyto.R.string.position;

/**
 * 个人信息 （昵称  职位）
 */
public class UserInfoActivity extends BaseActivity implements DnToolbar.OnLeftTitleClickListener, DnToolbar.OnRightTitleClickListener {

    @Bind(R.id.toolbar)
    DnToolbar mToolbar;
    @Bind(R.id.userCenterNickname)
    EditText mUserCenterNickname;
    @Bind(R.id.userCenterWeibo)
    EditText mUserCenterWeibo;
    @Bind(R.id.userCenterQq)
    EditText mUserCenterQq;
    @Bind(R.id.linear)
    LinearLayout mLinear;
    @Bind(R.id.lineResumeTwo)
    LinearLayout mLineResumeTwo;
    @Bind(R.id.lineResumeThree)
    LinearLayout mLineResumeThree;
    private String mContent;
    private int mTitle;

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_info;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        Bundle bundle = getIntent().getExtras();
        mTitle = bundle.getInt(AppConstant.INFOTITLE);
        mContent = bundle.getString(AppConstant.INFODATA);
        mToolbar.setMainTitleRightText(R.string.person_save_text);
        mToolbar.setOnLeftTitleClickListener(this);
        mToolbar.setOnRightTitleClickListener(this);
        switch (mTitle) {
            case 1:
                mLinear.setVisibility(View.VISIBLE);
                mLineResumeTwo.setVisibility(View.GONE);
                mLineResumeThree.setVisibility(View.GONE);
                mToolbar.setMainTitle(R.string.nickname);
                mUserCenterNickname.setText(null == mContent ? "" : mContent);
                break;
            case 2:
                mLinear.setVisibility(View.GONE);
                mLineResumeTwo.setVisibility(View.VISIBLE);
                mLineResumeThree.setVisibility(View.GONE);
                mToolbar.setMainTitle(position);
                mUserCenterWeibo.setText(null == mContent ? "" : mContent);
                break;
            case 3:
                mLinear.setVisibility(View.GONE);
                mLineResumeTwo.setVisibility(View.GONE);
                mLineResumeThree.setVisibility(View.VISIBLE);
                mToolbar.setMainTitle(R.string.position);
                mUserCenterQq.setText(null == mContent ? "" : mContent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onLeftClick(View view) {
        finish();
    }

    @Override
    public void onRightClick(View view) {
        Map<String, String> map = MapHelper.getAccessMap();
        map.put("hrId", GlobalApplication.getInstance().getUserID());
        if (mTitle == 1) {
            if (!StringUtils.isEmpty(mUserCenterNickname.getText().toString().trim())) {
                map.put("nickname", mUserCenterNickname.getText().toString().trim());
            } else {
                showShortToast(R.string.nikname_nonempty);
            }
        } else if (mTitle == 2) {
            map.put("position", mUserCenterWeibo.getText().toString().trim());
        } else {
            finish();
        }
        saveData(map);
    }
    // 上传数据
    private void saveData(Map<String, String> map) {
        Api.getInstance().getProxy(ApiService.class).updateUserInfoData(Api.getCacheControl(), map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new RxSubscriber<UserDataBean>(this, true) {
                    @Override
                    protected void _onNext(UserDataBean userDataBean) {
                        InfoRefreshBean bean = new InfoRefreshBean();
                        bean.setFlag(mTitle);
                        if (mTitle == 1) {
                            bean.setContent(mUserCenterNickname.getText().toString().trim());
                        } else if (mTitle == 2) {
                            bean.setContent(mUserCenterWeibo.getText().toString().trim());
                        }
                        //rxbus 刷新我的信息界面
                        RxBus.getInstance().post(AppConstant.INFOREFRESH, bean);
                        finish();
                    }

                    @Override
                    protected void _onError(String message) {
                        showShortToast(message);
                    }
                });
    }
}
